<?php
	class KhachSan extends CI_Controller
	{
		function admin_index()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{		
				$this->load->model('khachsan_model');		
				$data['khachsan'] = $this->khachsan_model->LayTatCaKhachSan_NhaCungCap();
				
				$data['key'] = NULL;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/khachsan_view', $data);
			}
			else
				redirect('login/admin_index');
		}
		
		function Tim()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$key = $this->input->post('key');
				if(!$key)
					$key = urldecode($this->uri->segment(3));
				
				$this->load->model('khachsan_model');
				$data['khachsan'] = $this->khachsan_model->Tim($key);
				
				$data['key'] = $key;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/khachsan_view', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function XemChiTiet()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$maks = $this->uri->segment(3);
				if($maks)
				{
					$this->load->model('khachsan_model');
					$data['khachsan'] = $this->khachsan_model->LayKhachSan_NhaCungCap($maks);
					
					$this->load->model('hinhanh_model');
					$data['hinhanh'] = $this->hinhanh_model->LayHinhAnh($maks);					
										
					$data['key'] = NULL;
				
					$data['chucvu'] = $this->session->userdata('chucvu');
				
					$data['current_username'] = $this->session->userdata('username');		
					
					$this->load->view('admin/chitiet_khachsan_view', $data);
				}
			}
			else
				redirect('login/admin_index');			
		}

		function MoTrangCapNhat()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$maks = $this->uri->segment(3);
				if($maks)
				{
					$this->load->model('khachsan_model');
					$data['khachsan'] = $this->khachsan_model->LayKhachSan_NhaCungCap($maks);
					
					$this->load->model('hinhanh_model');
					$data['hinhanh'] = $this->hinhanh_model->LayHinhAnh($maks);			
										
					$data['key'] = NULL;
				
					$data['chucvu'] = $this->session->userdata('chucvu');
				
					$data['current_username'] = $this->session->userdata('username');		
					
					$this->load->view('admin/capnhat_khachsan_view', $data);
				}
			}
			else
				redirect('login/admin_index');
		}
		
		function CapNhat()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$maks = $this->input->post('maks');
				$data = array(
								'Ten' => $this->input->post('tenks'),
								'SoNha' => $this->input->post('sonha'),
								'Duong' => $this->input->post('duong'),
								'Quan' => $this->input->post('quan'),
								'Tinh_ThanhPho' => $this->input->post('thanhpho'),
								'QuocGia' => $this->input->post('quocgia'),
								'Email' => $this->input->post('email'),
								'DienThoai' => $this->input->post('dienthoai'),
								'ThongTin' => $this->input->post('thongtin'),
								'QuyDinh' => $this->input->post('quydinh'),
								'TieuChuan' => $this->input->post('tieuchuan'),
								'Diem' => $this->input->post('diem'),
								'VAT' => $this->input->post('VAT'),
								'PhiDichVu' => $this->input->post('phidichvu')/1000,
								'HoaHong' => $this->input->post('hoahong'),
							);
				$this->load->model('khachsan_model');
				$result = $this->khachsan_model->CapNhat($data, $maks);
				
				$this->load->model('hinhanh_model');
				$upload_result = $this->hinhanh_model->DoUpload($maks);
				
				if($result < 0 || $upload_result < 0)
				{
					?>
                    <script>
						alert('Thay đổi thông tin thất bại!');
						window.location = "<?php echo base_url(); ?>index.php/khachsan/MoTrangCapNhat/<?php echo $maks; ?>";						
					</script>
                    <?php
					return;
				}
				
				?>
                <script>
					alert('Thông tin đã được thay đổi!');
					window.location = "<?php echo base_url(); ?>index.php/khachsan/MoTrangCapNhat/<?php echo $maks; ?>";						
				</script>
                <?php				
			}
			else
				redirect('login/admin_index');
		}
		
		function XoaHinh()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$maha = $this->input->post('maha');
				$maks = $this->input->post('maks');				
				if($maha && $maks)
				{
					$this->load->model('hinhanh_model');
					$result = $this->hinhanh_model->Xoa($maha);
					
					if($result < 0)
					{
						?>
    	                <script>
							alert('Xóa hình ảnh thất bại!');
							window.location = "<?php echo base_url(); ?>index.php/khachsan/MoTrangCapNhat/<?php echo $maks; ?>";						
						</script>
                    	<?php
						return;						
					}
					
					$data['hinhanh'] = $this->hinhanh_model->LayHinhAnh($maks);
					$this->load->view('admin/ajax_hinhanh_khachsan', $data);
				}
			}
			else
				redirect('login/admin_index');			
		}
		
		function LayKhachSan_ajax()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$key = $this->input->post('name_startsWith');
				
				$this->load->model('khachsan_model');
				$data['maks'] = $this->khachsan_model->LayMaKS_ajax($key);
				$data['tenkhachsan'] = $this->khachsan_model->LayTenKhachSan_ajax($key);								
				$data['thanhpho'] = $this->khachsan_model->LayThanhPho_ajax($key);
				
				$this->load->view('admin/ajax_danhsachkhachsan', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function LayThanhPho_TenKhachSan_ajax()
		{
			$key = $this->input->post('name_startsWith');
				
			$this->load->model('khachsan_model');
			$data['maks'] = NULL;
			$data['tenkhachsan'] = $this->khachsan_model->LayTenKhachSan_Category_ajax($key);								
			$data['thanhpho'] = $this->khachsan_model->LayThanhPho_Category_ajax($key);
				
			$this->load->view('admin/ajax_danhsachkhachsan', $data);		
		}
	}
?>